package com.jpyl.jpMedicalTreatment.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.model.NewsMessageModel;
import com.jpyl.jpMedicalTreatment.util.StringUtil;

import java.util.List;

/**
 * 首页列表适配器
 * Created by 15697 on 2019/1/4.
 */

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> implements View
        .OnClickListener {
    private List<NewsMessageModel.NewsModel> list;

    public NewsAdapter(List<NewsMessageModel.NewsModel> list) {
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_item_news,
                null);
        view.setOnClickListener(this);
        return new NewsAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        String date = StringUtil.getDateToString(Long.parseLong(list.get(position).date),
                StringUtil.format
        );
        holder.tv_time.setText("日期：" + date);
        holder.tv_title.setText(list.get(position).title);
        holder.itemView.setTag(position);
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void onClick(View view) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick((Integer) view.getTag());
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title, tv_time;

        ViewHolder(View convertView) {
            super(convertView);
            tv_title = convertView.findViewById(R.id.tv_itemNews_title);
            tv_time = convertView.findViewById(R.id.tv_itemNews_time);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    private OnItemClickListener mItemClickListener;

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

}
